package com.huawei.aiservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huawei.aiservice.entity.dto.FeedBackDto;
import com.huawei.aiservice.entity.pojo.FeedBack;
import com.huawei.aiservice.entity.vo.Response;
import com.huawei.aiservice.mapper.FeedBackMapper;
import com.huawei.aiservice.service.FeedBackService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FeedBackServiceImpl extends ServiceImpl<FeedBackMapper, FeedBack> implements FeedBackService {

    public Response<String> feedBack(FeedBackDto feedBackDto) {
        LambdaQueryWrapper<FeedBack> wrapper = Wrappers.<FeedBack>lambdaQuery().eq(FeedBack::getDefectId, feedBackDto.getDefectId());
        List<FeedBack> feedBacks = baseMapper.selectList(wrapper);

        if (feedBacks.isEmpty()) {
            // 插入
            FeedBack feedBack = new FeedBack(feedBackDto.getDefectId(), feedBackDto.getUserId(),
                    feedBackDto.getReportId(), feedBackDto.isAgree(), feedBackDto.isCancel(), feedBackDto.getReason());
            baseMapper.insert(feedBack);
        } else {
            // 更新
            FeedBack feedBack = feedBacks.get(0);
            feedBack.setAgree(feedBackDto.isAgree());
            feedBack.setCancel(feedBackDto.isCancel());
            feedBack.setReason(feedBackDto.getReason());
            baseMapper.updateById(feedBack);
        }
        return Response.success("success");
    }

    @Override
    public Response<FeedBack> getFeedBack(int defectId) {
        LambdaQueryWrapper<FeedBack> wrapper = Wrappers.<FeedBack>lambdaQuery().eq(FeedBack::getDefectId, defectId);
        List<FeedBack> feedBacks = baseMapper.selectList(wrapper);
        return Response.success(feedBacks.get(0));
    }
}
